nevopushfandomcom_ru-20200215-history
Android Push notification
Firebase Console https://console.firebase.google.com/?hl=ru&pli=1 Ссылки # Push уведомления в Android. Грабли, костыли и велосипеды # Google cloud messaging Documentation # Google Cloud Messaging: «Сова, открывай! Пуш пришел!» Разработка под Android*, Разраб FCM client setup Libraries don't have an application ID. Application ID is supplied by the consumer of your library. Just include the dependency and that's it. Don't apply the google services plugin on a library. Your client would have presumably followed https://firebase.google.com/docs/cloud-messaging/android/client tutorial. Which means you have to create your library's push message processor to which your client will delegate work. As you can see connecting Firebase to your library is handled entirely by your library's consumer. You don't touch Firebase on client side at all. FCM server setup You need an application ID to setup FCM. One application ID can only be associated to one Firebase Project, which would be that of your library's consumer. Dead end. If you're running an online service and need to send push messages on behalf of your client you need them to create a Server key (restricted to your server's IP address) in Google Dev console and upload it along with Project ID to your server. These would allow your server to send push messages. This is essentially your client giving you permission to send push mesages on their behalf. Mind potential security risks and abuse of your service. Creating Firebase library project 1. can create a firebase project for your library. 2. do-not add the google-service.json to the library.. each app should add its own 3. you can still create a token for the sender-id of your library-firebase-project using the API: firebaseInstanceId.getToken("sender-id-2", "FCM") 4. for the client setup the instructions above are correct. – ' Push Notification Types // There are two types of messages data messages and notification messages. '''Data messages' are handled // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app // is in the foreground. When the app is in the background an automatically generated notification is displayed. // When the user taps on the notification they are returned to the app. Messages containing both notification // and data payloads are treated as notification messages. The Firebase console always sends notification // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options ' ' Перехват различного пэйлоада // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); } // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); } ' ' Сборка приложения с пуш-уведомлениями ﻿Для сборки с помощью Gradle. Для сборки другими способами пропустить п. 1-3 и добавить либу google play services в приложение вручную. 1) В build.gradle уровня проекта добавить buildscript { dependencies { classpath 'com.google.gms:google-services:3.0.0' } } 2) В build.gradle уровня модуля добавить dependencies { compile 'com.google.android.gms:play-services-analytics:9.6.1' compile 'com.google.android.gms:play-services-gcm:9.6.1' } apply plugin: 'com.google.gms.google-services' 3) В папку модуля нужно положить скачанный из google firebase console файл google-services.json 4) В манифест приложения добавить: 4.1) Пермишны: android:name="com.nevosoft.tsa.permission.C2D_MESSAGE"" android:protectionLevel="signature" /> android:name="com.nevosoft.tsa.permission.C2D_MESSAGE" /> 4.2) Ресиверы и сервисы 5) Добавить содержимое папки res в res приложения 6) Положить файлы из java\com\nevosoft\gcm\ по соответствующему пути в приложение 7) Добавить код из java\com\nevosoft\main\AppActivity.java в тело главной Activity приложения 8) Проверить FIXME в коде из пп. 6-7. (Заменить имя класса главной Activity, добавить обработку отсыла regId на сервер и обработку пейлоада)